<template>
  <div class="banner">
    <h1>Banner管理</h1>
    <hr />
    <el-row class="banner-add">
      <el-button type="primary" @click="bannerAdd">添加数据</el-button>
    </el-row>
    <div class="banner-list">
      <el-table :data="bannerList" style="width: 100%">
        <el-table-column prop="id" label="编号" width="80"> </el-table-column>
        <el-table-column prop="cimage" label="轮播图" width="180">
          <template slot-scope="scope">
            <el-image :src="scope.row.cimage">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="200">
        </el-table-column>
        <el-table-column label="移动" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              class="el-icon-top"
              @click="topMove(scope.$index, scope.row)"
            ></el-button>
            <el-button
              size="mini"
              class="el-icon-bottom"
              @click="bottomMove(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column prop="count" label="点击量" width="80">
        </el-table-column>
        <el-table-column prop="create_time" label="添加时间"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            ></el-button>
            <el-button
              v-if="scope.row.is_enable"
              size="mini"
              type="primary"
              icon="el-icon-view"
              @click="handleLook(scope.$index, scope.row)"
            ></el-button>
            <el-button
              v-else
              size="mini"
              type="danger"
              icon="el-icon-view"
              @click="handleLook(scope.$index, scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="banner-page"
        background
        layout="prev, pager, next"
        :total="totalCurrent"
        :page-size="pageSize"
        @current-change="currentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
export default {
  name: "BannerView",
  data() {
    return {
      bannerList: [],
      page: 1,
      pageSize: 10,
      totalCurrent: 0,
    };
  },
  methods: {
    //上移
    topMove(index, row) {
      console.log("当前点击向上移动：", index, row);
      if (index == 0) {
        this.$message({
          type: "warning",
          message: "已经到顶部了哦！",
        });
      } else {
        console.log(
          "当前点击的上一条数据下标：",
          this.bannerList[index - 1].id
        );
        this.move(row.id, this.bannerList[index - 1].id);
      }
    },
    //下移
    bottomMove(index, row) {
      console.log("当前点击向下移动：", index, row);
      if (index == this.bannerList.length - 1) {
        this.$message({
          type: "warning",
          message: "已经到底部了哦！",
        });
      } else {
        console.log(
          "当前点击的下一条数据下标：",
          this.bannerList[index + 1].id
        );
        this.move(row.id, this.bannerList[index + 1].id);
      }
    },
    //优先级请求
    move(id1, id2) {
      this.$http({
        url: "/banner/move",
        method: "GET",
        params: {
          exId1: id1,
          exId2: id2,
        },
      })
        .then((res) => {
          console.log("请求成功：", res);
          this.getInit(this.page, this.pageSize);
        })
        .catch((err) => {
          console.log("请求失败：", err);
        });
    },
    //编辑
    handleEdit(index, row) {
      console.log(index, row);

      this.$router.push({
        path: "/banner/add",
        query: { row: row },
      });
    },
    //是否可见
    handleLook(index, row) {
      console.log(index, row);

      this.$http({
        url: "/banner/updateEnable",
        method: "GET",
        params: {
          id: row.id,
          isEnable: !row.is_enable,
        },
      })
        .then((res) => {
          console.log("请求成功：", res);

          row.is_enable = !row.is_enable;
        })
        .catch((err) => {
          console.log("请求失败：", err);
        });
    },
    //删除
    handleDelete(index, row) {
      console.log("当前点击：", index, row.id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$http({
            url: "/banner/del",
            method: "GET",
            params: {
              id: row.id,
            },
          })
            .then((res) => {
              console.log("请求成功：", res);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getInit(this.page, this.pageSize);
            })
            .catch((err) => {
              console.log("请求失败：", err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增
    bannerAdd() {
      this.$router.push("/banner/add");
    },
    //列表数据渲染
    getInit(page, pageSize) {
      this.$http({
        url: "/banner/indexAll",
        method: "GET",
        params: {
          page,
          pageSize,
        },
      })
        .then((res) => {
          console.log("请求成功：", res);

          this.bannerList = res.data.data;
          this.totalCurrent = res.data.count;
        })
        .catch((err) => {
          console.log("请求失败：", err);
        });
    },
    //切换分页重新渲染
    currentChange(page) {
      console.log("当前页：", page);
      this.page = page;
      this.getInit(page, this.pageSize);
    },
  },
  created() {
    this.getInit(this.page, this.pageSize);
  },
};
</script>

<style lang="scss" scoped>
.banner {
  min-width: 1100px;
}
h1 {
  margin: 10px 10px;
}

.banner-add {
  margin: 10px 10px;
}

.banner-page {
  text-align: center;
  margin-top: 10px;
}
</style>
